package com.itheima.Demo03_login_anli;

import com.itheima.jdbcUtils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class demo01 {
    //模拟用户登录 请用户输入 密码和账户 查询数据库中是否包含 用户输入的用户名和密码

    //测试方法
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String u = sc.next();
        System.out.println("请输入密码:");
        String p = sc.next();

        method(u,p);
    }

    //定义一个方法查询用户输入的信心是否存在
    public static void method(String uname ,String pwd){
        //使用工具包 查询数据库所有用户信息
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet  = null;
        try {
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM user ");
            while (resultSet.next()){
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                if (uname.equals(username)&&pwd.equals(password)){
                    System.out.println("恭喜你！登录成功！");
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {//释放资源
            JDBCUtils.Close(connection,statement,resultSet);
        }

    }
}
